nodewidth=46;

nodeflagperl = new Array();
for(i=0;i<80;i++)
{
	nodeflagperl[i] = 1;
	//1:wait for burning 
	//0:seleted to burn
	//2:blink
	//3:done successfully
	//4:done unsuccessfully
}



function selectOnes()
{

}


function set_dot(i,j)// when clicked ,change color of dots
{
	var c=document.getElementById("coordiv");
	var cxt=c.getContext("2d");
	cxt.fillStyle="#A9A9A9";
	cxt.beginPath();
	cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle="#000000";
	cxt.font="16px Arial";
	cxt.fillText(""+(i*10+j),15+i*40-7,15+j*40+4);
}

function cancel_dot(i,j)// when clicked ,change color of dots
{
	var c=document.getElementById("coordiv");
	var cxt=c.getContext("2d");
	cxt.fillStyle="#FF0000";
	cxt.beginPath();
	cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle="#000000";
	cxt.font="16px Arial";
	cxt.fillText(""+(i*10+j),15+i*40-7,15+j*40+4);
}
function checkID()
{
	if(document.myform.showid.value=="")
	{
		alert("Please choose a node at least!");
		return false;
	}
	var str=prompt("Please input password","******");
	if(str!="tinyos")
	{
		alert("Wrong Password!");
		return false;
	}

	return true;
}

function stateChanged()
{
	if (xmlHttp.readyState==4)
	{
		set_dot(6,6);
	//	alert(xmlHttp.responseText);
	}
}


function checkblinkfuc(nodestr){
	var nodestrarray = new Array();
	nodestrarray= nodestr.split(",");
	var num;
	var count;
	for(count=0;count<nodestrarray.length;count++){
		num = parseInt(nodestrarray[count]);
		nodeflagperl[num]=2;
	}

}

function blinkfuc(flag,nodeid){//flag==1:#ff0000 0:#00ff00
	var c=document.getElementById("coordiv");
	var cxt=c.getContext("2d");
	var i= Math.floor(nodeid/10);
	var j= nodeid%10; 
	if(flag==1){
		cxt.fillStyle="#FF0000";
	}else if(flag==0)
	{
		cxt.fillStyle="#00FF00";
	}else if(flag==3)
	{
		cxt.fillStyle="#00FF00";
	}else if(flag==4){
		cxt.fillStyle="#0000FF";
	}
cxt.beginPath();
cxt.arc(15+i*40,15+j*40,15,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();
cxt.fillStyle="#000000";
cxt.font="16px Arial";
cxt.fillText(""+(i*10+j),15+i*40-7,15+j*40+4);
}

flag=0;
function blinkallfuc(){
	flag++;
	var k=0;
	for(k=0;k<80;k++){
		if(nodeflagperl[k]==2){
			blinkfuc(flag%2,k);
		}
	}
}
function returnstate(responseText)
{

	//debug
	var str=null;
        for(count=0;count<80;count++){
	str+=nodeflagperl[count]+",";
	}
	//alert(str);

	var responseArray =  new Array();
	responseArray= responseText.split(",");
	var num;
	var count;
	for(count=0;count<responseArray.length;count+=2){
		num = parseInt(responseArray[count]);
		nodeflagperl[num]=parseInt(responseArray[count+1])+3;
		//change states ,so 0->3,1->4
	}
	//debug
	var str=null;
        for(count=0;count<80;count++){
	str+=nodeflagperl[count]+",";
}	
	//alert(str);

}

function showstate()
{
	var count=0;
	for(count=0;count<80;count++){
		if(nodeflagperl[count]==3){
			blinkfuc(3,count);
		}else if(nodeflagperl[count]==4){
			blinkfuc(4,count);
		}
	}
}

function opencomm()
{

	var str=prompt("Please input password","******");
	if(str!="tinyos")
	{
		alert("Wrong Password!");
		return false;
	}
	var xmlHttp=new XMLHttpRequest();
	if (xmlHttp==null)
	{
		alert ("no support AJAX！");
		return;
	}
	var url="opencomm_1.jsp";

	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 && xmlHttp.status==200)
		{
			alert(xmlHttp.responseText);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;

}
function closecomm()
{

	var str=prompt("Please input password","******");
	if(str!="tinyos")
	{
		alert("Wrong Password!");
		return false;
	}
	var xmlHttp=new XMLHttpRequest();
	if (xmlHttp==null)
	{
		alert ("no support AJAX！");
		return;
	}
	var url="closecomm_1.jsp";

	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 && xmlHttp.status==200)
		{
			alert(xmlHttp.responseText);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;

}


function show()   //show init color


function show()   //show init color
{
	var j;
	for(var i =0 ; i<8 ; i++)
	{
		for( j =0 ; j<10 ; j++)
		{
			var c=document.getElementById("coordiv");
			var cxt=c.getContext("2d");
			cxt.fillStyle="#A9A9A9";
			cxt.beginPath();
			cxt.arc(15+i*nodewidth,15+j*nodewidth,10,0,Math.PI*2,true);
			cxt.closePath();
			cxt.fill();
			cxt.fillStyle="#000000";
			cxt.font="13px Arial";
			cxt.fillText(""+(i*10+j),15+i*nodewidth-7,15+j*nodewidth+4);
		}
	}
}

function getNode()
{

	//element=document.getElementById('myimage')

	//element.src="pic/downpic.jpg";


	var str="";
	for(i=0;i<80;i++)
	{
		if(nodeflagperl[i]==0)
		{
			str+=i;
			str+=",";
		}
	}
	//document.write(str);

	var c=document.getElementById('showid');
	c.value=str; 


}

function cnvs_getCoordinates(e) //when clicked ,get i,j
{
	x=e.clientX-100;
	y=e.clientY-80;

	for(var i=0;i<8;i++)
	{
		for(var j=0;j<10;j++)
		{
			tmpx=15+40*i;
			tmpy=15+40*j;
			if(((x-tmpx)*(x-tmpx)+(y-tmpy)*(y-tmpy))<=15*15)
			{
				var nodeid=i*10+j;
				if(nodeflagperl[nodeid]==1)
				{
					nodeflagperl[nodeid]=0;
					cancel_dot(i,j);
				}
				else if(nodeflagperl[nodeid]==0)
				{
					nodeflagperl[nodeid]=1;
					set_dot(i,j);
				}


				break;
			}
		}

	}
	document.getElementById("xycoordinates1").innerHTML="Clicked Node ID:" + nodeid ;
	//	document.getElementById("xycoordinates").innerHTML="Clicked Node ID:::" + nodeid ;
}


function sensor_show(e) //when clicked ,get i,j
{
	x=e.clientX-100;
	y=e.clientY-80;

	for(var i=0;i<8;i++)
	{
		for(var j=0;j<10;j++)
		{
			tmpx=15+40*i;
			tmpy=15+40*j;
			if(((x-tmpx)*(x-tmpx)+(y-tmpy)*(y-tmpy))<=15*15)
			{
				var nodeid=i*10+j;
				if(nodeflagperl[nodeid]==1)
				{
					nodeflagperl[nodeid]=0;
					cancel_dot(i,j);
				}
				else if(nodeflagperl[nodeid]==0)
				{
					nodeflagperl[nodeid]=1;
					set_dot(i,j);
				}


				break;
			}
		}

	}
}
 color=new Array("#408933","#FF097F","#5CACEE","#228822","#0F6EB4","#FF3030","#DEB807","#68AF95","#0AFF70","#9F79EE","#8DEEEE","#8B8B00","#050505","#80CDCD","#EECBAD","#AAAAAA","#66CDAA","#54FF9F","#F0B2AA","#436EEE","#4EEE94","#EE9023","#309023","#09EE70","#887800","#770512","#5397B0","#C08912","#d2d409","#679012","#e9b033","#905620");
function line(nodeidi,nodeidj,colornum)
{
var xi= Math.floor(nodeidi/10);
var yi=nodeidi%10;
var xj= Math.floor(nodeidj/10);
var yj=nodeidj%10;

  var c=document.getElementById("coordiv");
  var ctx=c.getContext("2d");
  ctx.strokeStyle=color[colornum];
  ctx.lineWidth=1;
  ctx.moveTo(xi*nodewidth+15,yi*nodewidth+15);
  ctx.lineTo(xj*nodewidth+15,yj*nodewidth+15);
  ctx.closePath();
  ctx.stroke();
}

function circle(nodeid,colornum)
{
var x= Math.floor(nodeid/10);
var y=nodeid%10;

  var c=document.getElementById("coordiv");
  var ctx=c.getContext("2d");
  var colorint=parseInt(colornum);
  ctx.fillStyle=color[colorint];
  ctx.beginPath();
  ctx.arc(15+x*nodewidth,15+y*nodewidth,10,0,Math.PI*2,true);
  ctx.closePath();
  ctx.fill();
  ctx.fillStyle="#000000";
  ctx.font="13px Arial";
  ctx.fillText(""+nodeid,15+x*nodewidth-7,15+y*nodewidth+4);
}
function cnvs_clearCoordinates()  //when leave are ,clear x,y
{
	document.getElementById("xycoordinates").innerHTML="";
}
function printData(e)
{

	document.getElementById("xycoordinates").innerHTML="ccc";
}
i
